package cn.gongwebo.payview.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;

/**
 * Created by GongWenBo on 2017/12/13.
 */

public class PayView extends View {

    private Paint mPaint;
    // 个数
    public static int COUNT = 6;
    // 弧度
    private float mRadius;
    // 输入的个数
    private int   IMPORT_COUNT;

    public PayView(Context context) {
        this(context, null);
    }

    public PayView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PayView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.parseColor("#CFCFCF"));
        mPaint.setStrokeWidth(3);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        mRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, dm);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        // 画最外层
        RectF rectF = new RectF(0, 0, width, height);
        mPaint.setColor(Color.parseColor("#DFDFDF"));
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawRoundRect(rectF, mRadius, mRadius, mPaint);
        // 画竖线
        int avgWidth = width / COUNT;
        for (int i = 0; i < COUNT; i++) {
            if (i != 0) {
                canvas.drawLine(avgWidth * i, 0, avgWidth * i, height, mPaint);
            }
        }
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.BLACK);
        if (IMPORT_COUNT != 0) {
            // 画小圆点
            for (int i = 0; i < IMPORT_COUNT; i++) {
                int cx = avgWidth * i + avgWidth / 2;
                int cv = height / 2;
                canvas.drawCircle(cx, cv, mRadius, mPaint);
            }
        }
    }

    public void setImportCount(int count) {
        IMPORT_COUNT = count;
        invalidate();
    }

    public int getImportCount() {
        return IMPORT_COUNT;
    }
}
